package org.lanqiao.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.lanqiao.myutil.Myutils;
import org.lanqiao.orm.EleBeforeBill;
import org.lanqiao.orm.EleOpeartor;
import org.lanqiao.orm.EleUser;
import org.lanqiao.service.INetUserFun;
import org.lanqiao.service.IOpeatorFun;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/Op")
public class OpControl {
	@Resource
	IOpeatorFun opService;
	@Resource
	INetUserFun netUserService;

	@RequestMapping(value = "/adduser.action")
	public String addUser() {
		// 跳转到添加用户的界面
		return "opeartor/addUser";
	}

	@RequestMapping(value = "/insertUser.action")
	public String insertUser(HttpSession session, EleUser user) {
		// 调用添加用户的方法
		boolean result = opService.insertUser(user);
		if (result) {
			session.setAttribute("addUser", "添加成功");
		} else {
			session.setAttribute("addUser", "添加失败");
		}
		return "opeartor/addUser";
	}

	@RequestMapping(value = "/userPay.action")
	public String userPay() {
		// 条跳转到缴费管理的界面
		return "opeartor/userPay";
	}

	@RequestMapping(value = "/selectUser.action")
	public String selectUser(HttpSession session, String card) {
		// 查询用户的信息及其余额和要消费的情况
		EleUser user = opService.selectUser(card);
		if (user.getUserid() > 0) {
			float allCoast = opService.selectCoast(user.getUserid().toString());
			session.setAttribute("allCoast", allCoast);
		}
		session.setAttribute("payman", user);
		return "opeartor/userPay";
	}

	@RequestMapping(value = "/payMoney.action")
	public String payMoney(HttpSession session, String addmoney) {
		// 调用处理缴费的方法
		EleUser user = (EleUser) session.getAttribute("payman");
		Float allCoast = (Float) session.getAttribute("allCoast");
		if (allCoast == null) {
			allCoast = 0f;
		}
		EleOpeartor op = (EleOpeartor) session.getAttribute("opeartor");
		float remainMoney = opService.payMoney(user, addmoney, allCoast, op.getOpid());
		session.setAttribute("remainMoney", remainMoney);
		return "opeartor/userPay";
	}

	@RequestMapping(value = "/queryBill.action")
	public String queryBill() {
		// 条跳转到缴费管理的界面
		return "opeartor/queryUserBill";
	}

	@RequestMapping(value = "/queryUserBill.action")
	public String queryUserBill(HttpSession session, String card, String start, String end) {
		// 用户的账单的查询
		EleUser user = opService.selectUser(card);
		List<EleBeforeBill> allBill = (List) netUserService.userBillList(user.getUserid(), Myutils.normTime(start),
				Myutils.normTime(end));
		session.setAttribute("beforeBill", allBill);
		if (allBill != null) {
			float allBillMoney = 0;
			for (EleBeforeBill bill : allBill) {
				allBillMoney = allBillMoney + bill.getUpmoney();
			}
			session.setAttribute("allBillMoney", allBillMoney);
		}
		return "opeartor/queryUserBill";
	}

	@RequestMapping(value = "/insertRe.action")
	public String insertRe(HttpSession session, String remain) {
		// 存入余额
		EleUser user = (EleUser) session.getAttribute("payman");
		EleOpeartor op = (EleOpeartor) session.getAttribute("opeartor");
		Float allCoast = (Float) session.getAttribute("allCoast");
		Float allc = Myutils.changeToFloat(remain);
		allCoast += allc;
		boolean result = opService.inserRe(user, allCoast.toString(), op.getOpid());
		if (result) {
			session.setAttribute("insertRe", "存入成功");
			EleUser userNew = opService.selectUser(user.getUsercard());
			session.setAttribute("payman", userNew);
			session.setAttribute("remainMoney", 0);
		} else {
			session.setAttribute("insertRe", "存入失败");
		}
		session.removeAttribute("allCoast");
		return "opeartor/userPay";
	}

}
